package gameobjects
{
	import flash.display.Sprite;
	
	public class Bar extends Sprite
	{
		public var colour:uint = 0x00FF00;
		private var object:GameObject;
		
		private var redMask:uint = 0x010000;
		private var greenMask:uint = 0x000100;
		private var a:int;
		private var b:int;
		
		public function Bar(object:GameObject,a:int,b:int)
		{
			super();
			this.object = object;
			this.a = a;
			this.b = b;
			object.addChild(this);
			update();
		}
		
		public function draw(x:int):void
		{
			this.graphics.clear();
			calcColour();
			this.graphics.beginFill(colour);

			this.graphics.drawRect(a, b, 15+x, 5);
			this.graphics.endFill();
		}
		
		private function percentage():Number
		{
			return object.getHealth() / object.getMaxHealth();
		}
		
		public function update():void
		{
			draw(percentage()*50);
		}
		
		private function calcColour():void
		{
			var pom:Number = percentage()*100;
			
			if(pom>=65)
			{
				colour = 0x00FF00 + 7 * redMask * (100-pom);
			}
			if(pom<65 && pom>30)
			{
				colour = 0xFFFF00 - 7 * greenMask * (100-pom); 
			}
			if(pom <=30)
			{
				colour = 0xFF0000;
			}
			
		}
		
		
	}
}
